package com.mhwx.mhlive.ui.fragment;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.mhwx.mhlive.R;
import com.mhwx.mhlive.ui.activity.SearchActivity;
import com.mhwx.mhlive.ui.adapter.TabAdapter;
import com.mhwx.mhlive.ui.base.BaseFragment;
import com.mhwx.mhlive.util.IntoActivity;
import com.mhwx.mhlive.util.TablayouUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 类功能描述
 * 主页
 * @author Guol
 * @version 1.0
 * @copyright 北京明华维新信息技术有限公司
 * @time 2017/4/21
 */
public class ChoiceFragment extends BaseFragment implements View.OnClickListener {
    @BindView(R.id.tabLayout)
    TabLayout mTabLayout;

    @BindView(R.id.viewpager)
    ViewPager viewpager;

    @BindView(R.id.img_search)
    ImageView img_search;
  private TabAdapter adapter;

    public static final String[] tabTitle = new String[]{"热门", "教育", "医疗", "汽车", "旅游", "电商"};
    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_choice;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initFragment();
    }
    private void initFragment(){

        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < tabTitle.length; i++) {
            if (tabTitle[i].equals("热门")){
                 fragments.add(HotFragment.newInstance(i));
            }else if (tabTitle[i].equals("教育")||tabTitle[i].equals("医疗")||
                    tabTitle[i].equals("汽车")||tabTitle[i].equals("旅游")){
                fragments.add(MedicalFragemnt.newInstance(i));
            }else if ( tabTitle[i].equals("电商")){
                fragments.add(RetailersFragment.newInstance(i));
            }
            else{
                fragments.add(ChoiceItemFrgament.newInstance(i + 1));
            }
        }

        adapter = new TabAdapter(getChildFragmentManager(), fragments,tabTitle);
        viewpager.setOffscreenPageLimit(tabTitle.length );
        //给ViewPager设置适配器
        viewpager.setAdapter(adapter);
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(viewpager);
        //设置可以滑动
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        TablayouUtil.setIndicator(getContext(), mTabLayout, 15, 15);

        img_search.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
         switch (view.getId()){
             case R.id.img_search:
                 IntoActivity.toNextActivity(getContext(), SearchActivity.class);
                 break;
         }
    }
}
